package com.qfedu.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/19 10:54
 * @Modified:
 */
public class AliSmsUtil {
    public static final String TEM_MSG="SMS_181550693";
    //密码找回
    public static final String TEM_FCODE="SMS_204975067";
    //注册模板
    public static final String TEM_RCODE="SMS_262430180";
    //登录模板
    public static final String TEM_LCODE="SMS_475840072";
    /**必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。*/
   // private static final String SMS_KEY="LTAI5tRy4QwR5K9PLT7BGsSt";
    private static final String SMS_KEY="LTAI5t5fxYR9iMZ9pWTYPZDt";
    /**必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。*/
    //private static final String SMS_ECRET="u7PaUWfcDu6pMOM6Pq1ialsSMmX3zt";
    private static final String SMS_ECRET="CFKwIORSwWi0c3qjwi8P3JAqTT3pq0";

    /**
     * 阿里云短信客户端对象 */
    private static Client client;
    static {
        Config config = new Config()
                .setAccessKeyId(SMS_KEY)
                .setAccessKeySecret(SMS_ECRET);
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client= new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送短信验证码
     * @param template 短信模板
     * @param phone 手机号
     * @param code 验证码
     * @return 是否发送成功*/
    public static boolean sendCode(String template,String phone,String code){
        //1.创建请求对象
        SendSmsRequest request = new SendSmsRequest()
                .setSignName("社区短信")
                .setTemplateCode(template)
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        //2.执行请求 获取响应
        try {
       SendSmsResponse response=client.sendSmsWithOptions(request,new com.aliyun.teautil.models.RuntimeOptions());
//            //3.验证当前请求是否成功
           return response.getBody().getCode().equals("OK");
          //  return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
